#!/usr/bin/env python
# coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

# ak密钥
client = AcsClient('LTAI4GGEttmtG9CF3WT1vsG7', '4MydZqNC3zMl53IVk9o9sAcVSzQJVD', 'cn-hangzhou')


class Sms_send(object):

    # 单例模式，所有的短信发送只用一个对象
    _instance = None

    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super().__new__(cls, *args, **kwargs)

        return cls._instance

    def sms_aliyun(self, phone_num, sms_code):
        request = CommonRequest()
        request.set_accept_format('json')
        request.set_domain('dysmsapi.aliyuncs.com')
        request.set_method('POST')
        request.set_protocol_type('https')  # https | http
        request.set_version('2017-05-25')
        request.set_action_name('SendSms')

        request.add_query_param('RegionId', "cn-hangzhou")
        # 要发送的手机号
        request.add_query_param('PhoneNumbers', phone_num)
        # 短信签名
        request.add_query_param('SignName', "有畏无问新闻网")
        # 模板id
        request.add_query_param('TemplateCode', "SMS_204296255")
        # 发送的验证码格式：json
        request.add_query_param('TemplateParam', "{\"code\":\"%s\"}" % sms_code)

        response = client.do_action(request)
        # python2:  print(response)
        return str(response, encoding='utf-8')


if __name__ == '__main__':
    sms_send = Sms_send()
    resp = sms_send.sms_aliyun('15008167363', '124666')
    print(resp)
